package com.antdesignbackend.backend.warn.warning.controller;

import com.antdesignbackend.backend.animalNumber.entity.AnimalNumber;
import com.antdesignbackend.backend.animalNumber.service.AnimalNumberService;
import com.antdesignbackend.backend.common.CalendarDate;
import com.antdesignbackend.backend.entity.Enterprise;
import com.antdesignbackend.backend.entity.Production;
import com.antdesignbackend.backend.entity.Reproduction;
import com.antdesignbackend.backend.entity.Users;
import com.antdesignbackend.backend.entity.entityVo.EnterpriseVo;
import com.antdesignbackend.backend.service.EnterpriseService;
import com.antdesignbackend.backend.service.ProductionService;
import com.antdesignbackend.backend.service.ReproductionService;
import com.antdesignbackend.backend.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 预警信息
 *
 * @author 不二
 * @email warning
 * @date 2020-07-28 11:29:02
 */

@Controller
@RequestMapping("/warning")
@Slf4j
public class WarningController extends BaseController {

    private static final String FUN_NAME = "预警信息";//功能名称

    @Autowired
    EnterpriseService enterpriseService;

    @Autowired
    ProductionService productionService;

    @Autowired
    ReproductionService reproductionService;

    @Autowired
    UserService userService;
    @Autowired
    AnimalNumberService animalNumberService;

    @GetMapping("/index")
    @AuthToken
    public String index() {
        return "warn/warning/index";
    }


    @PostMapping("/tableSearch")
    @ResponseBody
    ReturnVO tableSearch(@RequestBody EnterpriseVo enterpriseVo) throws SecurityException, IllegalArgumentException {
        ReturnVO result = new ReturnVO();

        Enterprise enterprise = enterpriseService.getById(getLoginEnterpriseId());
        //获得企业存栏数
        int live = 0;
        QueryWrapper<AnimalNumber> wrapper = new QueryWrapper<>();
        wrapper.eq("Eid",enterprise.getEnterpriseId());
        List<AnimalNumber> list = animalNumberService.list(wrapper);
        if(!list.isEmpty()){
            for (AnimalNumber animalNumber:list){
                live += animalNumber.getSpecies()
                        + animalNumber.getReserve()
                        + animalNumber.getMenstruation()
                        + animalNumber.getLactation()
                        + animalNumber.getConservation()
                        + animalNumber.getCommodity()
                        + animalNumber.getLivestock();
            }
        }

        /**
         * 计算本季度死淘率 季度   死淘 / 调入 + 调出 + 死淘 + 出生
         */
        double mortality =0.00;
        Double m = 0.0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //得到本季度第一天时间
        Date firstQuarter = CalendarDate.getFirstDateOfSeason(new Date());
        //得到本季度最后一天时间
        Date lastQuarter = CalendarDate.getLastDateOfSeason(new Date());
        //获得本季度的生产记录
        QueryWrapper<Production> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("eid", getLoginEnterpriseId());
        queryWrapper.between("createTime", firstQuarter, lastQuarter);
        List<Production> productionList = productionService.list(queryWrapper);
        if (productionList.size() > 0) {
            //死淘
            Double dead = 0.0;
            //总计
            Double sum = 0.0;
            for (Production production : productionList) {
                //死淘
                if ("死淘".equals(production.getSituation())){
                    dead += production.getMale() + production.getFemale()
                            + production.getBreedFemale() + production.getLactation()
                            + production.getConservation() + production.getGrow();
                }
                sum += production.getMale() + production.getFemale()
                        + production.getBreedFemale() + production.getLactation()
                        + production.getConservation() + production.getGrow();
            }
            //计算死淘率
            m = dead * 100.0 / sum;
        }
        mortality = m;
        /**
         * 计算预产期超出数 当前时间大于预产时间，并且分娩日期为null
         */
        QueryWrapper<Reproduction> reproductionQueryWrapper = new QueryWrapper<>();
        reproductionQueryWrapper.eq("eid",getLoginEnterpriseId());
        reproductionQueryWrapper.lt("productionTime",new Date()).isNull("deliveryTime");
        List<Reproduction> reproductionList = reproductionService.list(reproductionQueryWrapper);
        enterpriseVo.setEnterpriseName(enterprise.getEnterpriseName());
        enterpriseVo.setAnimal(enterprise.getAnimal());
        enterpriseVo.setLive(live);
        enterpriseVo.setMortality(mortality);
        enterpriseVo.setOut(reproductionList.size());
        List<EnterpriseVo> list1 = new ArrayList<>();
        list1.add(enterpriseVo);
        result.setData(list1);
        return result;

    }

    /**
     * 显示最新的一条数据
     * @param enterpriseVo
     * @return
     * @throws SecurityException
     * @throws IllegalArgumentException
     */
    @PostMapping("/tableSearch2")
    @ResponseBody
    ReturnVO tableSearch2(@RequestBody EnterpriseVo enterpriseVo) throws SecurityException, IllegalArgumentException {
        ReturnVO result = new ReturnVO();

        Enterprise enterprise = enterpriseService.getById(getLoginEnterpriseId());
        //获得企业存栏数
        int live = 0;
        QueryWrapper<AnimalNumber> wrapper = new QueryWrapper<>();
        wrapper.eq("Eid",enterprise.getEnterpriseId());
        List<AnimalNumber> list = animalNumberService.list(wrapper);
        if(!list.isEmpty()){
            for (AnimalNumber animalNumber:list){
                live += animalNumber.getSpecies()
                        + animalNumber.getReserve()
                        + animalNumber.getMenstruation()
                        + animalNumber.getLactation()
                        + animalNumber.getConservation()
                        + animalNumber.getCommodity()
                        + animalNumber.getLivestock();
            }
        }

        /**
         * 计算本季度死淘率 季度   死淘 / 调入 + 调出 + 死淘 + 出生
         */
        double mortality =0.00;
        Double m = 0.0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //得到本季度第一天时间
        Date firstQuarter = CalendarDate.getFirstDateOfSeason(new Date());
        //得到本季度最后一天时间
        Date lastQuarter = CalendarDate.getLastDateOfSeason(new Date());
        //获得本季度的生产记录
        QueryWrapper<Production> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("eid", getLoginEnterpriseId());
        queryWrapper.between("createTime", firstQuarter, lastQuarter);
        List<Production> productionList = productionService.list(queryWrapper);
        if (productionList.size() > 0) {
            //死淘
            Double dead = 0.0;
            //总计
            Double sum = 0.0;
            for (Production production : productionList) {
                //死淘
                if ("死淘".equals(production.getSituation())){
                    dead += production.getMale() + production.getFemale()
                            + production.getBreedFemale() + production.getLactation()
                            + production.getConservation() + production.getGrow();
                }
                sum += production.getMale() + production.getFemale()
                        + production.getBreedFemale() + production.getLactation()
                        + production.getConservation() + production.getGrow();
            }
            //计算死淘率
            m = dead * 100.0 / sum;
        }
        mortality = m;
        /**
         * 计算预产期超出数 当前时间大于预产时间，并且分娩日期为null
         */
        QueryWrapper<Reproduction> reproductionQueryWrapper = new QueryWrapper<>();
        reproductionQueryWrapper.eq("eid",getLoginEnterpriseId());
        reproductionQueryWrapper.lt("productionTime",new Date()).isNull("deliveryTime");
        List<Reproduction> reproductionList = reproductionService.list(reproductionQueryWrapper);
        enterpriseVo.setEnterpriseName(enterprise.getEnterpriseName());
        enterpriseVo.setAnimal(enterprise.getAnimal());
        enterpriseVo.setLive(live);
        enterpriseVo.setMortality(mortality);
        enterpriseVo.setOut(reproductionList.size());
        result.setData(enterpriseVo);
        return result;
    }
}
